package br.urca.impl;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.HabilitacaoDAO;
import br.urca.model.Habilitacao;
import br.urca.model.HabilitacaoPK;

@Transactional
public class JPAHabilitacaoDAO implements HabilitacaoDAO {
	
	private EntityManager manager;
	
	public JPAHabilitacaoDAO() {
		this(null);
	}
	
	@Inject
	public JPAHabilitacaoDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public Habilitacao buscarPorId(HabilitacaoPK id) {
		try {
			return this.manager
					.createQuery(
							"SELECT h FROM Habilitacao h "
									+ "WHERE h.id.codCurso = :codCurso "
									+ "AND h.id.codHabilitacao = :codHabilitacao ",
							Habilitacao.class)
					.setParameter("codCurso", id.getCodCurso())
					.setParameter("codHabilitacao", id.getCodHabilitacao())
					.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

}
